package com.yoyoyo666.report.job;

import com.yoyoyo666.report.common.DateUtils;
import com.yoyoyo666.report.common.IDUtils;
import com.yoyoyo666.report.mapper.ReportContentMapper;
import com.yoyoyo666.report.mapper.ReportSendLogMapper;
import com.yoyoyo666.report.mapper.ReportUserMapper;
import com.yoyoyo666.report.model.ReportSendLog;
import com.yoyoyo666.report.model.ReportUser;
import com.yoyoyo666.report.service.SendMailService;
import com.yoyoyo666.report.service.SmsService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.TimeZone;

public class SendMailJob {

    @Autowired
    private ReportContentMapper contentMapper;

    @Autowired
    private ReportUserMapper userMapper;

    @Autowired
    private SendMailService sendMailService;

    @Autowired
    private ReportSendLogMapper sendLogMapper;

    @Autowired
    private SmsService smsService;


    private static DateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
    static{
        sdf.setTimeZone(TimeZone.getTimeZone("GMT+08:00"));
    }
    public void sendMail(){

        Calendar d = Calendar.getInstance();

        if(!DateUtils.isWork(d.getTime())){
            return;
        }

        String start = sdf.format(d.getTime());
        d.add(Calendar.DAY_OF_YEAR,1);
        String end = sdf.format(d.getTime());

        List<ReportUser> dayIncrement = userMapper.getDayIncrement(start, end);

        for (ReportUser reportUser : dayIncrement) {
            if(reportUser.getCnt()==null||reportUser.getCnt()==0){

                if(StringUtils.isBlank(reportUser.getMobile())){
                    continue;
                }
                //smsService.send(reportUser.getMobile(),reportUser.getName());
                System.out.println(reportUser.getName() + "今天没有更改日报");
            }else {
                try {
                    sendMailService.sendMail(reportUser.getId());
                } catch (Exception e) {
                    sendLogMapper.insert(new ReportSendLog(IDUtils.getId(),reportUser.getId(),"false",""));
                    e.printStackTrace();
                }
            }
        }

        System.out.println("123123123" + contentMapper);
    }



}
